/**
 * 用户登录判断、退出登录
*/
import {request} from '@/api/request/index.js'
import local_userinfo from '@/util/user/local-userinfo.js'

export default {
	checkloginstate(callback,noLogincall) {
// 		let wanttoUrlCopy = encodeURIComponent(wanttoUrl)
// 		let noLogin = () => {
// 			let jumpUrl = ''
// 			// #ifdef H5
// 			jumpUrl = '../../pages/login/common'
// 			// #endif
// 			// #ifndef H5
// 			jumpUrl = '../../pages/login/quick'
// 			// #endif
// 			uni.navigateTo({
// 				url: jumpUrl + '?redirect=' + wanttoUrlCopy
// 			})
// 		}
		let d = local_userinfo.getUserinfo();
		if (d) {
			uni.showLoading({
				mask:true
			})
			request.get({
					data: {
						c: 'LoginController',
						m: 'checkLogin',
						rediskey: d.sessionid,
						phone: d.phone
					}
				})
				.then((res) => {
					if (res.Status == 'ok') {
						if(callback) callback()
					} else {
						if(noLogincall) noLogincall()
					}
				})
				.catch(error => {
					if(noLogincall) noLogincall()
				});
		} else if(noLogincall) noLogincall()
	},
	logout() {
		let successCall = (sessionkey) => {
			uni.showLoading({
				mask:true
			})
			request.get({
					data: {
						c: 'LoginController',
						m: 'logout',
						rediskey: sessionkey
					}
				})
				.then((res) => {
					uni.switchTab({
						url: '../../pages/tabBar/index'
					})
				})
				.catch(error => {
					noLogin();
				});
		}
		let d = local_userinfo.getUserinfo();
		let sessionkey = d.sessionid
		if (sessionkey) {
			local_userinfo.delUserinfo();
			successCall(sessionkey)
		}
	},
}
